package com.clt.ess.service.impl;

import com.clt.ess.base.Constant;
import com.clt.ess.bean.ZTree;
import com.clt.ess.entity.Department;
import com.clt.ess.entity.User;
import com.clt.ess.service.*;
import com.clt.ess.utils.FastJsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;


@Service
public class MainServiceImpl implements IMainService {

    @Autowired
    private IUserService userService;
    @Autowired
    private IUnitService unitService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IPowerService powerService;

    @Autowired
    private IErrorLogService errorLogService;
    @Autowired
    private ILogService logService;

    /**
     * 首页登录处理方法
     * @param session
     * @param personId 登录的personId
     * @param userId userIdm
     * @return
     */
    @Override
    public ModelAndView indexHandle(HttpSession session,String personId, String userId) {
        //视图对象
        ModelAndView modelAndView = new ModelAndView();
        //设置返回视图
        modelAndView.setViewName("main");
        //根据personId查找用户表
        User user = new User();
        user.setPersonId(personId);
        //状态有效的登录身份
        user.setState(Constant.STATE_YES);
        //查找符合条件的用户
        List<User> userList =  userService.findLoginUserByPersonId(user);

        if(userList.size()!=0 ){
            //判断访问参数userId是否为空
            if("".equals(userId)||userId==null){
                //userId为空时 是 第一次访问。
                //此时自动分配第一个用户身份
                user = userList.get(0);
            }else{
                //userId不为空时，为拥有多个身份的用户切换时
                //根据其身份列表选出其切换的身份。
                for(User nav :userList){
                    if(nav.getUserId().equals(userId)){
                        user = nav;
                    }
                }
            }
            //将用户身份存入session，作为登录信息。
            session.setAttribute("user",user);

            modelAndView.addObject("user", user);

            modelAndView.addObject("userList", userList);

            //根据当前登录用户获取其单位列表结构
            List<ZTree> tree =  unitService.queryUnitMenu(user.getUnitId());
            if(tree==null){
                errorLogService.addErrorLog("MainController-index-unitNameChain获取单位列表结构出错！");
                modelAndView.setViewName("error");
                modelAndView.addObject("message", "获取单位列表结构出错");
            }else{
                modelAndView.addObject("unit_menu", FastJsonUtil.toJSONString(tree));
            }
            //单位名称chain
            String unitNameChain = unitService.getUnitNameChain(user.getUnitId());
            if("".equals(unitNameChain)||unitNameChain==null){
                errorLogService.addErrorLog("MainController-index-unitNameChain获取单位名称链出错！");
                modelAndView.setViewName("error");
                modelAndView.addObject("message", "获取单位名称链出错");
            }else{
                //放入当前单位名称chain，先清除。此处放入单位名称chain是为了写入系统日志是方便取用
                session.removeAttribute("unitNameChain");
                session.setAttribute("unitNameChain", unitNameChain);
            }

        }else{
            errorLogService.addErrorLog("MainController-index-userList根据personId查询的user为空！");
            modelAndView.setViewName("error");
        }
        return modelAndView;
    }

    /**
     * 初始化单位首页面
     * @return
     */
    @Override
    public ModelAndView initUnitPage(HttpSession session,String openUnitId) {
        //视图对象
        ModelAndView modelAndView = new ModelAndView();
        //设置返回视图
        modelAndView.setViewName("unit/unit_page");
        //判断是否有进入当前单位的权限
        if(powerService.checkIsHaveThisRangePower(session,unitService.findUnitById(openUnitId),Constant.topUnitLevel)){
            //放入当前单位名称chain，先清除。此处放入单位名称chain是为了写入系统日志是方便取用
            session.removeAttribute("unitNameChain");
            session.setAttribute("unitNameChain", unitService.getUnitNameChain(openUnitId));

            //查询要进入的单位数据
            modelAndView.addObject("unit",  unitService.findUnitById(openUnitId));

            //根据要打开的单位ID查询当前单位下的部门列表
            Department department = new Department();
            department.setUnitId(openUnitId);
            department.setState(Constant.STATE_YES);
            modelAndView.addObject("departmentList",  departmentService.findDepartmentList(department));

            //判断当前登录用户对于各个按钮的权限
            //审核
            if(powerService.getPowerForButton(session,"makeSealPower_auditSeal")){
                modelAndView.addObject("reviewButton",  true);
            }else {
                modelAndView.addObject("reviewButton",  true);
            }
            //制作
            if(powerService.getPowerForButton(session,"makeSealPower_makeSeal")){
                modelAndView.addObject("makeButton",  true);
            }else {
                modelAndView.addObject("makeButton",  true);
            }
            //注册UK
            if(powerService.getPowerForButton(session,"makeSealPower_registerTheExistingUkSeal")){
                modelAndView.addObject("registerButton",  true);
            }else {
                modelAndView.addObject("registerButton",  true);
            }
        }else {
            modelAndView.addObject("message",  "您不能访问此单位");
            modelAndView.setViewName("error");
        }
        return modelAndView;
    }
}
